<?php

class ArticleModel extends Model
{

    /**
     * 发布日志
     * 数据保存到数据库
     *
     * @param type $data
     */
    public function addArticle($data)
    {
        $title = $data['title'];
        $content = $data['content'];
        $addtime = $data['addtime'];
        $catid = $data['catid'];
        $sql = "INSERT INTO s_article (title, content, addtime, catid) 
              VALUES ('{$title}', '{$content}', '{$addtime}', {$catid})";
        $row = $this->execute($sql);
        return $row;
    }

    /**
     * 获取所有的日志
     */
    public function getAllItems()
    {
        //a.* a表的所有字段 article表的别名
        $sql = "SELECT a.*, c.name FROM s_article a LEFT JOIN s_category c ON a.catid = c.id ORDER BY a.id DESC";
        $row = $this->query($sql);
        return $row;
    }

    /**
     * 获取某一条日志的内容
     */
    public function getDetailById($id)
    {
//
        $sql = "SELECT a.*, c.name FROM s_article a LEFT JOIN s_category c ON a.catid = c.id WHERE a.id = {$id}";
        $row = $this->query($sql, FALSE);
        return $row;
    }

//   删除一条日志
    public function delete($id)
    {
        $sql = "DELETE FROM s_article WHERE id = {$id}";
        $row = $this->execute($sql);
        return $row;
    }
}
